<template>
    <div>
        <nav-com title="修改密码" :showView='showView' @back="backHandler()"></nav-com>
        <div class="margin_box">
            <van-form @submit="onSubmit">
                <van-row class="padd_box">
                    <van-col span="24">
                        <div class="login_box" style=" ">
                            <label>旧密码：</label>
                            <input type="password" v-model='loginfrom.password'   placeholder="请填写您的密码" />
                        </div>
                    </van-col>
                </van-row>
                <van-row class="padd_box">
                    <van-col span="24">
                        <div class="login_box" style=" ">
                            <label>新密码：</label>
                            <input type="password" v-model='loginfrom.newpassword'  placeholder="请设置您的密码" />
                        </div>
                    </van-col>
                </van-row>
                <van-row class="padd_box">
                    <van-col span="24">
                        <div class="login_box" style=" ">
                            <label>确认密码：</label>
                            <input type="password" v-model='loginfrom.newpassword2' @blur="newpasswordHanlder(loginfrom.newpassword2)"  placeholder="请输入您设置的密码" />
                        </div>
                    </van-col>
                </van-row>
                <van-row>
                    <van-col style="text-align:center; font-size:4vw; color:red; margin-top:2.6vw;" span="24">
                        {{message}}
                    </van-col>
                </van-row>
                <van-row class="padd_box" >
                    <van-col span="24">
                        <van-button style="font-size:4.8vw; height:13.33vw;" color="#FF491F" type="info" native-type='submit' round  block>确认修改</van-button>
                    </van-col>
                </van-row>
                
            </van-form>
        </div>
    </div>
</template>
<script>
import {postLogin,postVerificationcodes,resetpwd2 }  from '@/api/api'
import navCom from '@/component/nav'
import {Toast} from "vant";
export default {
    name:'login',
    components:{
        navCom
    },
    data() {
        return {
            message:'',
            loginfrom:{
                password:'',
                newpassword:'',
                newpassword2:'',
            },
            showView:true,
            disabled:false,
            codeText:'获取验证码',
            codeFalg:true,
            checkbox:false,
        }
    },
    methods:{
        //返回
        backHandler(){
            if(weixin.isAndroid()){
                window.JS_TROOPS.goBack()
            }else{
                this.$router.go(-1);
            }
        },
        //
        agreementHanlder(){
            this.$router.push('/agreement')
        },
        //验证码
        newpasswordHanlder(val){
            if(val==""){
                this.message = '再次确认密码为空'
                return false
            }else if(this.loginfrom.newpassword!=val){
                this.message = '两次输入密码不一致'
                this.loginfrom.newpassword2="";
                return false
            }else{
                this.message = ''
                return true
            }

        },
        
        //保存
        onSubmit(){

            if(this.loginfrom.newpassword!=this.loginfrom.newpassword2){
                this.message = '两次输入密码不一致'
                this.loginfrom.newpassword2="";
                return false
            }

            let data = {
                mobile:this.loginfrom.cellphone,
                captcha:this.loginfrom.verificationCode,
                type:"0"
            }
            resetpwd2(this.loginfrom).then(res=>{
                console.log("resetpwd2",res)
                if(res.data.code == 1){
                    Toast(res.data.msg)
                    localStorage.removeItem("shareUser");
                    window.weixin.removeToken()
                    if(weixin.isAndroid()){
                            window.JS_TROOPS.dealLoginOut()
                        }
                    let time = new Date().getTime()
                    
                    this.$router.push('/login?time='+time)
                }else{
                    this.$dialog.alert({
                            message:res.data.msg
                     })
                }
            })
        },
        //去注册
        registerHandler(){
            this.$router.push('/register')
        },
        //获取验证码
        codeHandler(){
            let that = this
            let telLen = this.loginfrom.cellphone.length
            if(telLen==11){
                let time = null
                clearInterval(time)
                that.codeFalg = false
                let t = 60
                time = setInterval(function(){
                    t--
                    that.codeText = t
                    if(t<=0){
                        that.codeText = "获取验证码"
                        that.codeFalg = true
                        clearInterval(time)
                    }
                },1000)

                postVerificationcodes({mobile:this.loginfrom.cellphone,event:'mobilelogin'}).then(res=>{
                    if(res.data.code == 200){

                    }else{
                        this.$dialog.alert({
                            message:res.data.msg
                        })
                    }
                })

                return true
            }else{
                this.message = '请输入正确手机号'
                return false
            }
            
        },
    }
}
</script>
<style scoped>
.margin_box {margin-left:10vw; margin-right:10vw; margin-top:11.2vw;}
.padd_box {min-height:10.66vw;  margin-top:3.4vw;  border-radius: 1.333vw;}
.ashBg {background: #fff; border-radius: 25px; border:1px solid #e3e3e3; width:100%;}
.login_box {border:1px solid #e3e3e3; font-size:4.2vw; border-radius: 6.66vw; height:13.3vw; line-height:13.3vw;}
.login_box label {padding-left:5.3vw; }
.login_box input { height:11vw; line-height: 11vw; width:50vw; border:none;}
</style>